package de.eventdroid;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.StringTokenizer;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import de.eventdroid.helper_classes.GeoDataHandler;
import de.eventdroid.helper_classes.JSONHandler;
import de.eventdroid.helper_classes.RESTHandler;

import android.app.Activity;
import android.content.Intent;
import android.net.ParseException;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

@SuppressWarnings("rawtypes")
public class LocationSpecialResultScreen extends Activity {
	
	private ArrayList<String> aktFoundObjectsForListViewArray = new ArrayList<String>();
	private HashMap intentParameters = new HashMap();
	private String resultString = null;
	private String specLocationId = "";
	private StringTokenizer locDataStrings;
	private double location_latitude = 0.0;
	private double location_longitude = 0.0;
	private String location_website = "";
	private String location_e_mail = "";
	private Intent handleIntent = new Intent();
	private GeoDataHandler aktGeoHandler = new GeoDataHandler();
	private JSONObject locationCategoryJSON;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.eventdroid_location_special_result);
        this.setSpecLocationFields();
    }
    
    /** Click Event Handler for the buttons on the Startscreen */
    public void onHomeClick(final View view) {    	
    	switch (view.getId()) {
    	case R.id.btn_to_home: 
    		startActivity(new Intent(this, StartScreen.class));
    		break;
    	}
    } 
    
    /** Click Event Handler for the buttons on the Startscreen */
    public void onFurtherOperationsClick(final View view) {
    	/* check which button was pushed */
    	switch (view.getId()) {
    	
	    	case R.id.btn_location_handle_geo:
	    		/* if geodata has to be visualized */
	    		if((location_latitude != 0.0) && (location_longitude != 0.0) ) {
			    	/* Put the necessary Parameters into the Map */
			    	intentParameters.put("latitude", this.location_latitude );
			    	intentParameters.put("longitude", this.location_longitude );
			    	
			    	/* Start the Request */
			    	startActivity(aktGeoHandler.createExternalDataExchange("google_maps", intentParameters));
	    		} else {
	    			Toast toast = Toast.makeText(getApplicationContext(), "Es wurden keine Geodaten hinterlegt", Toast.LENGTH_SHORT);
	    			toast.show();
	    		}
		    break;
		    	
	    	case R.id.btn_location_open_website:
	    		
	    		if(!location_website.equals("null")) {
	    		/* if a website has to be opened */
	    		handleIntent = new Intent(Intent.ACTION_VIEW,
	    				Uri.parse("http://"+location_website));
	    		startActivity(handleIntent);
	    		} else {
	    			Toast toast = Toast.makeText(getApplicationContext(), "Es wurde keine Internetadresse hinterlegt", Toast.LENGTH_SHORT);
	    			toast.show();
	    		}
	    		
	    		
	    	break;
	    	
	    	case R.id.btn_location_mail:
	    		/* if a email program has to be opened */
	    		if(!location_e_mail.equals("null")) {
	    			handleIntent = new Intent(Intent.ACTION_VIEW,
	    					Uri.parse("mailto:"+location_e_mail));
	    			startActivity(handleIntent);
	    		} else { 
	    			Toast toast = Toast.makeText(getApplicationContext(), "Es wurde keine E-Mail Adresse hinterlegt", Toast.LENGTH_SHORT);
	    			toast.show();
	    		}
		    break;
    	}
    }
    
    /**
     * Fill the Fields of the Activity Views with the result values
     */
    private void setSpecLocationFields() {
    	
    	/* Fetch assigned data of the special Location */
    	specLocationId = getIntent().getExtras().getString("location_data");
    	
    	/* Fetch the data of the specialLocation from REST Server */
    	resultString = RESTHandler.getData("locationHandler/id/"+specLocationId);
    	
		/* Parse the JSON String into  */
    	aktFoundObjectsForListViewArray = JSONHandler.parseJSONDataToArrayList(resultString, "location", true);		
		
    	/* Tokenizer for the fetched Data */
    	locDataStrings = new StringTokenizer(aktFoundObjectsForListViewArray.get(0), "#");
		
		try{
			/* TextViews which have to be set */
			String location_name = locDataStrings.nextToken();
			String location_adress = locDataStrings.nextToken();
			String location_zip_town = locDataStrings.nextToken() + " " + locDataStrings.nextToken();
			String location_description = locDataStrings.nextToken();
			String location_telephone = locDataStrings.nextToken();
			String location_fax = locDataStrings.nextToken();
			
			/* fill the empty strings with placeholderdata */
    		if (location_telephone.equals("") || location_telephone.equals("0") || location_telephone.equals("null")) {
    			location_telephone = "n/a";
    		}
    		if (location_fax.equals("") || location_fax.equals("0") || location_fax.equals("null")) {
    			location_fax = "n/a";
    		}
			
			((TextView)findViewById(R.id.lbl_spec_location_name)).setText(location_name);
			((TextView)findViewById(R.id.lbl_spec_location_adress)).setText(location_adress);
			((TextView)findViewById(R.id.lbl_spec_location_zip_town)).setText(location_zip_town);
			((TextView)findViewById(R.id.lbl_spec_location_description)).setText(location_description);
			((TextView)findViewById(R.id.lbl_spec_location_telephone)).setText(location_telephone);			
			((TextView)findViewById(R.id.lbl_spec_location_fax)).setText(location_fax);
			location_e_mail = locDataStrings.nextToken();
			location_website = locDataStrings.nextToken();
			location_longitude = Double.parseDouble(locDataStrings.nextToken());
			location_latitude = Double.parseDouble(locDataStrings.nextToken());
			
			/* Get the data for the selected spinner position */
    		String location_category = RESTHandler.getData("locationHandler/categories/"+specLocationId);
    		
			locationCategoryJSON = new JSONObject(location_category);
			location_category = locationCategoryJSON.getString("location_category_name");
			((TextView)findViewById(R.id.lbl_spec_location_category)).setText(location_category);
			
			
		} catch (ParseException e1) {
			Log.e("Parsing Exception", "Parsing Exception when filling Resultscreen: "+e1.toString());
		} catch (JSONException e) {
			Log.e("JSON Parsing Exception", "Parsing Exception when filling Resultscreen: "+e.toString());
		}
    	
    }
    
    /**
     * Creation Class for the menu
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	MenuInflater inflater = getMenuInflater();
    	inflater.inflate(R.menu.menu, menu);
    	return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	
    	switch (item.getItemId()) {
    		case R.id.menu_startscreen:     
    			startActivity(new Intent(this, StartScreen.class));           	
    			break;
    	}
    	return true;
    }
    
}